/*
 ============================================================================
 Name		: BackgroundMonitor.cpp
 Author	  : Lenka Litvova
 Version	 : 1.0
 Copyright   : Copyright belongs to Lenka Litvova
 Description : CBackgroundMonitor implementation
 ============================================================================
 */

#include "BackgroundMonitor.h"
//#include "APGCLI.H"

CBackgroundMonitor::CBackgroundMonitor(MEventObserver *aObserver) :
	CActive(EPriorityStandard), // Standard priority
	iObserver(aObserver),
	iTaskList(CCoeEnv::Static()->WsSession())
	{
	}

CBackgroundMonitor* CBackgroundMonitor::NewLC(MEventObserver *aObserver)
	{
	CBackgroundMonitor* self = new (ELeave) CBackgroundMonitor(aObserver);
	CleanupStack::PushL(self);
	self->ConstructL();
	return self;
	}

CBackgroundMonitor* CBackgroundMonitor::NewL(MEventObserver *aObserver)
	{
	CBackgroundMonitor* self = CBackgroundMonitor::NewLC(aObserver);
	CleanupStack::Pop(); // self;
	return self;
	}

void CBackgroundMonitor::ConstructL()
	{
	User::LeaveIfError(iTimer.CreateLocal()); // Initialize timer	
	User::LeaveIfError(iLs.Connect());
	CActiveScheduler::Add(this); // Add to scheduler
	}

CBackgroundMonitor::~CBackgroundMonitor()
	{
	if(iChecked)
	{
		delete iChecked;
		iChecked = NULL;
	}
	Cancel(); // Cancel any request, if outstanding
	iTimer.Cancel();
	iTimer.Close(); // Destroy the RTimer object
	iLs.Close();
	// Delete instance variables if any
	}

void CBackgroundMonitor::DoCancel()
	{
	iCancel = ETrue;
	}

void CBackgroundMonitor::StartL(CArrayFixFlat<TUid> *aMonitored)
	{
	iCancel = EFalse;
	iAll = EFalse;
	if(iChecked)
		delete iChecked;
	iChecked = NULL;
	iChecked = new (ELeave) CArrayFixFlat<TUid>(10);
	if(iMonitored)
	{
		delete iMonitored;
		iMonitored = NULL;
	}
	iMonitored = aMonitored;
	//Cancel(); // Cancel any request, just to be sure
	iTimer.After(iStatus, 1000000); // Set for later
	SetActive(); // Tell scheduler a request is active
	}

void CBackgroundMonitor::Continue()
	{
		iCancel = EFalse;
		iTimer.After(iStatus, 1000000); // Set for later
		SetActive(); // Tell scheduler a request is active
	}

void CBackgroundMonitor::StartL()
	{
	iCancel = EFalse;
	iAll = ETrue;
	//Cancel(); // Cancel any request, just to be sure
	iTimer.After(iStatus, 1000000); // Set for later
	SetActive(); // Tell scheduler a request is active
	}

void CBackgroundMonitor::RunL()
	{
	TBool found = EFalse;
	if(iAll)
	{
		TInt count;
		iLs.AppCount(count);
	
		if(count > iLastCount)
			iObserver->ApplicationStarted();
		iLastCount = count;
	}
	else
	{
		for(TInt i=0; i<iMonitored->Count();i++)
		{
			TApaTask task = iTaskList.FindApp(iMonitored->At(i));
			if(task.Exists())
			{	
				found = ETrue;
				iObserver->ApplicationStarted(iMonitored->At(i));
				iChecked->AppendL(iMonitored->At(i));
				iMonitored->Delete(i);
			}
		}
		for(TInt i=0; i<iChecked->Count();i++)
		{
				TApaTask task = iTaskList.FindApp(iChecked->At(i));
				if(!task.Exists())
				{	
					iMonitored->AppendL(iChecked->At(i));
					iChecked->Delete(i);
				}
		}
	}
	if(!iCancel)
	{
		iTimer.After(iStatus, 2000000); // Set for 2 sec later
		SetActive(); // Tell scheduler a request is active
	}
	}

TInt CBackgroundMonitor::RunError(TInt aError)
	{
	return aError;
	}
